#!/bin/sh

uci -q batch <<-EOF >/dev/null
	delete ucitrack.@omr-bypass[-1]
	add ucitrack omr-bypass
	set ucitrack.@omr-bypass[-1].init=omr-bypass
	commit ucitrack
EOF

if [ -n "$(uci -q get omr-bypass.dpi)" ]; then
	proto=$(uci -q get omr-bypass.dpi.proto)
	[ -n "$proto" ] && {
		for pro in $proto; do
			uci -q batch <<-EOF >/dev/null
				set omr-bypass.dpi[-1]=dpi
				set omr-bypass.dpi[-1].proto=$pro
				set omr-bypass.dpi[-1].interface=all
			EOF
		done
		uci -q commit omr-bypass
	}
fi

if [ -n "$(uci -q get omr-bypass.ips)" ]; then
	ips=$(uci -q get omr-bypass.ips.ip)
	[ -n "$ips" ] && {
		for ip in $ips; do
			uci -q batch <<-EOF >/dev/null
				set omr-bypass.ips[-1]=ips
				set omr-bypass.ips[-1].ip=$ip
				set omr-bypass.ips[-1].interface=all
			EOF
		done
		uci -q commit omr-bypass
	}
fi

if [ -n "$(uci -q get dhcp.dnsmasq[0].ipset)" ]; then
	domains="$(uci -q get dhcp.dnsmasq[0].ipset | sed -e 's:/::' -e 's:/ss_rules_dst_bypass_all::' -e 's:/: :g')"
	[ -n "$domains" ] && {
		for domain in $domains; do
			uci -q batch <<-EOF >/dev/null
				set omr-bypass.domains[-1]=domains
				set omr-bypass.domains[-1].name=$domain
				set omr-bypass.domains[-1].interface=all
			EOF
		done
		uci -q commit omr-bypass
	}
fi

if [ "$(uci -q get ucitrack.@shadowsocks-libev[-1].affects | grep omr-bypass)" != "" ]; then
	uci -q batch <<-EOF >/dev/null
		del_list ucitrack.@shadowsocks-libev[-1].affects=omr-bypass
		commit ucitrack
	EOF
fi

if [ "$(uci -q get firewall.omr_bypass)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		set firewall.omr_bypass=include
		set firewall.omr_bypass.path=/etc/firewall.omr-bypass
		set firewall.omr_bypass.reload=1
		commit firewall
	EOF
fi

if [ "$(uci -q get omr-bypass.m6replay)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		set omr-bypass.m6replay=proto
		add_list omr-bypass.m6replay.url='m6web.fr'
		add_list omr-bypass.m6replay.url='6play.fr'
		add_list omr-bypass.m6replay.url='6cloud.fr'
		commit omr-bypass
	EOF
fi
if [ "$(uci -q get omr-bypass.mycanal)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		set omr-bypass.mycanal=proto
		add_list omr-bypass.mycanal.url='mycanal.fr'
		add_list omr-bypass.mycanal.url='canal-plus.com'
		add_list omr-bypass.mycanal.url='canalplus.com'
		add_list omr-bypass.mycanal.url='canalplus-cdn.net'
		add_list omr-bypass.mycanal.url='canalplus.pro'
		add_list omr-bypass.mycanal.url='canal-plus.net'
		commit omr-bypass
	EOF
fi
if [ "$(uci -q get omr-bypass.minecraft)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		set omr-bypass.minecraft=proto
		add_list omr-bypass.minecraft.url='authserver.mojang.com'
		commit omr-bypass
	EOF
fi
if [ "$(uci -q get omr-bypass.lesnumeriques)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		set omr-bypass.lesnumeriques=proto
		add_list omr-bypass.lesnumeriques.url='lesnumeriques.com'
		add_list omr-bypass.lesnumeriques.url='botscorner.com'
		add_list omr-bypass.lesnumeriques.url='app.botscorner.com'
		commit omr-bypass
	EOF
fi
if [ "$(uci -q get omr-bypass.disneyplus)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		set omr-bypass.disneyplus=proto
		add_list omr-bypass.disneyplus.url='bamgrid.com'
		add_list omr-bypass.disneyplus.url='disney-plus.net'
		commit omr-bypass
	EOF
fi
if [ ! -f /etc/crontabs/root ] || [ "$(cat /etc/crontabs/root | grep bypass)" = "" ]; then
	echo "0 2 * * * /etc/init.d/omr-bypass bypass_asn" >> /etc/crontabs/root
fi
rm -f /tmp/luci-indexcache
exit 0
